<link href="../practice/css/form.css" rel="stylesheet" type="text/css" />

<form enctype="multipart/form-data"  action="upload_save_image.php" method="post" >
  <input placeholder="Image title" type="text" name="title" required /><br>
  <input type="file" name="avatar"  required />
  <p><input type="submit" value="Continue &rarr;"/></p>
</form>
<?php
  require_once('../practice/includes/db.php');

pr($_POST);
  ////////////// START For Delete Checkbox ////////////
  if(isset($_POST['del'])){
    $ids = implode(',',$_POST['del']);
    var_dump($ids);
    deleteByIs('user_images', $ids);
    foreach($_POST['del_images'] as $values){
      @unlink("uploads/$values");
      @unlink("uploads/chats/$values");
      @unlink("uploads/wall/$values");
    }
  }


  ////////////// END For Delete Checkbox ////////////
###########################
  echo getMessage();
###########################

 //////// Search records //////////////////////
 $searchQuery = '';
  $search = isset($_GET['search'])?trim($_GET['search']):'';
  if($search != ''){
    $searchQuery = " WHERE `title` LIKE '%$search%'";
  }

  ///////////////////////////////////////////

  $url = $_SERVER['PHP_SELF'];
  //define('OFFSET',5); //a.k.a Number of records per page
  $page_num = isset($_GET['page']) ? $_GET['page'] : 1;
  $offset = 4;
  $index = ($page_num - 1)*$offset;
  $total_records =  countAll('user_images',$searchQuery);//from DB
  $num_of_pages = ceil($total_records / $offset);



  //////// Sort Records ///////////
  $sort = isset($_GET['sort'])?$_GET['sort']:'desc';
  $field = isset($_GET['field'])?$_GET['field']:'added_date';
  $order = "ORDER BY $field $sort";
  $newSort = ($sort == 'asc') ? 'desc' : 'asc';

  //////// UPDATE STATUS ///////
  $id = isset($_GET['id']) ? $_GET['id'] : 0;
  if(isset($_GET['status'])) {
    $status = $_GET['status'] == 0 ? 1 : 0;
    update('user_images',array('status'=>$status),"WHERE `id`='$id'");
  }
  //////////////////////////////
  $user_images = fetchAll('user_images',$searchQuery,"LIMIT $index,$offset", $order);
?>
<link href="css/form.css" rel="stylesheet" type="text/css" />

<!------------- Search form starts here --------------------->
<form action="" method="get">
  <table border="0" align="center" width="80%" cellpadding="5">
    <tr>
      <td width="30%"><input type="search" name="search" placeholder="Search Here.."/></td>
      <td><input type="submit"  align="left"  style="width: 120px;" value="Search !!"/></td>
    </tr>
  </table>
 </form>
<!------------- Search form ends here --------------------->
<form action="" method="post">
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <th>
        <input type="checkbox" class="parent" />

      </th>
      <th>#id</th>
      <th><a href="<?=$url?>?sort=<?=$newSort?>&field=title&page=<?=$page_num?>">Title</a></th>
      <th>Avatar</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
<?php
  $i = 1;
  while($user = mysql_fetch_assoc($user_images))
  {

    $color = ($i++ %2==0)?'lightyellow':'lightgreen';
    $id = $user['id'];
    $xtra= '&page='.$page_num;
    $status = showStatus($user['status'],$url,$id,$xtra);
?>
    <tr style="background-color:<?=$color?>">
      <td >
  <input value="<?=$id?>" type="checkbox" name="del[]" class="child" />
  <input value="<?=$user['avatar']?>" type="hidden" name="del_images[]" class="child" />

  </td>
      <td ><a target="_blank" href="update_image.php?id=<?=$id?>"><?=$id?></a></td>
      <td ><?=$user['title']?></td>
      <td ><img src="uploads/wall/<?=$user['avatar']?>"></td>
      <td ><?=$status?></a></td>
    </tr>
<?php
  }
?>
   <tr><td colspan="5"><input onClick="return confirm('Are you sure?')" type="submit" style="width:110px" value="Delete All"></td></tr>
  </tbody>
</table>
</form><!----- cLOSE Delete All Images Form--->
<hr>
<table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
  <thead>
    <tr>
      <?php for ($i = 1; $i <= $num_of_pages ; $i++) {
      if($i == $page_num) { ?>

      <th style="color: red;font-size: 25px;"><?=$i?></th>

      <?php }else{ ?>
      <th><a href="<?=$url?>?page=<?=$i?>"><?=$i?></a></th>
      <?php }//end else
    }//emd for()
     ?>
    </tr>
  </thead>
</table>


<script type="text/javascript" src="jquery-1.10.1.min.js"></script>

<script type="text/javascript">
  jQuery(document).ready(function(){


    $('.parent').on('click',function(){
      //alert($(this).is(':checked'))
      if($(this).is(':checked') == true) {
        $('.child').prop('checked',true);
      } else {
        $('.child').prop('checked',false);
      }
    });

    $('.child').on('click',function(){
      //alert($(this).is(':checked'))
      if($(this).is(':checked') == true) {

        if($('.child').size() == $('.child:checked').size()) {
          $('.parent').prop('checked',true);
        }

      } else {
        $('.parent').prop('checked',false);
      }
    });

    $('.container').on('click','.cross',function(){
      $(this).closest('div').remove();
      $('.container').find('.plus:last').show();
    });
  });
</script>

